﻿<template>
  <a-modal :title="title" width="40%" :visible="visible" :confirmLoading="loading" @ok="handleSubmit"
    @cancel="() => { this.visible = false;this.entity={}}">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="接口ID" prop="OptionId">
          <a-input v-model="entity.OptionId" autocomplete="off" value="1111" />
        </a-form-model-item>
        <a-form-model-item label="节点名称" prop="NodeNO">
          <a-input v-model="entity.NodeNO" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="父项编号" prop="ParentNO">
          <!-- <a-input v-model="entity.ParentNO" autocomplete="off" /> -->
          <a-select show-search placeholder="请选择" option-filter-prop="children" style="width: 100%"
            :filter-option="filterOption" @change="parentNO_handleChange" v-model="entity.ParentNO">
            <a-select-option v-for="item in GetNodes()" :value="item.NodeNO" :key="item.id">
              {{ item.NodeNO }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="目标字段名称" prop="TargetField">
          <a-input v-model="entity.TargetField" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="映射类型" prop="MapType">
          <a-select show-search placeholder="请选择" option-filter-prop="children" style="width: 100%"
            :filter-option="filterOption" @change="MapType_handleChange" v-model="entity.MapType">
            <a-select-option value="V">
              使用值
            </a-select-option>
            <a-select-option value="D">
              使用源数据
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="目标数据类型" prop="TargetFieldType">
          <!-- <a-input v-model="entity.TargetFieldType" autocomplete="off" /> -->
          <a-select show-search placeholder="请选择" option-filter-prop="children" style="width: 100%"
            :filter-option="filterOption" @change="targetFieldType_handleChange" v-model="entity.TargetFieldType">
            <a-select-option value="String">
              String
            </a-select-option>
            <a-select-option value="Int">
              Int
            </a-select-option>
            <a-select-option value="Object">
              Object
            </a-select-option>
            <a-select-option value="Array">
              Array
            </a-select-option>
            <a-select-option value="Bool">
              Bool
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="源字段名/值" prop="SourceField">
          <a-input v-model="entity.SourceField" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="是否启用" prop="IsEnable">
          <a-select show-search placeholder="请选择" option-filter-prop="children" style="width: 100%"
            :filter-option="filterOption" @change="IsEnable_handleChange" v-model="entity.IsEnable">
            <a-select-option value="Y">
              Y
            </a-select-option>
            <a-select-option value="N">
              N
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="查询键" prop="SreachKey">
          <a-input v-model="entity.SreachKey" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="排序" prop="Sort">
          <a-input v-model="entity.Sort" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Notes">
          <a-input v-model="entity.Notes" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      routeQuery: this.parentObj.$data.routeQuery
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title,selectedRows) {
      this.init()
      if (id) {
        this.loading = true
        this.$http.post('/Base_Manage/Base_InterfaceOptionEntry/GetTheData', { id: id }).then(resJson => {
          this.loading = false;
          this.entity = resJson.Data;
        })
      }
      else {
        if (selectedRows.length > 0 && title==='Add') {
          this.entity=JSON.parse(JSON.stringify(selectedRows[0]))
          this.entity.Id='';
          this.entity.OptionId =  this.routeQuery.keyword;
        }
        }
        //通过路由传过来的父页面ID
        
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true

        this.$http.post('/Base_Manage/Base_InterfaceOptionEntry/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },

    //获取父节点名称列表供选择
    GetNodes() {
      var list = [];
      this.parentObj.$data.data.forEach((value, key) => {
        var o = {};
        if (value.TargetFieldType === 'Object' || value.TargetFieldType === 'Array') {
          o.NodeNO = value.NodeNO;
          o.Id = value.Id;
          list.push(o);
        }
      }
      );
      list.push('');
      return list
    },

    //select控件查询过滤方法
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      );
    },
    //select控件选择赋值方法
    parentNO_handleChange(value) {
      var list = this.GetNodes();
      list.forEach((v, k) => {
        if (v.NodeNO === value) { this.entity.ParentId = v.Id; }
      });
    },
    targetFieldType_handleChange(value) {
      this.entity.TargetFieldType = value;
    },
    IsEnable_handleChange(value) {
      this.entity.IsEnable = value;
    },
    MapType_handleChange(value) {
      this.entity.MapType = value;
    },
  }
}
</script>
